/*
 * @Author: 15956982113 493885262@qq.com
 * @Date: 2025-05-28 14:09:11
 * @LastEditors: 15956982113 493885262@qq.com
 * @LastEditTime: 2025-05-30 13:17:30
 * @FilePath: \v3-webpack\src\utils\used.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { Toast, showToast, showLoadingToast, closeToast } from 'vant';

const used = {
	name: 'used',
	router: null, // 用于保存注入的 router 实例
	stores: {}, // 存储注入的 store 实例
	// 设置 router
	setRouter(router) {
		this.router = router;
	},
	// 注册单个 store
	setStore(key, store) {
		this.stores[key] = store;
	},
	//获取缓存
	getStorageSync(key) {
		return localStorage.getItem(key)
	},
	//设置缓存
	setStorageSync(key, val) {
		return localStorage.setItem(key, val)
	},
	//删除缓存
	removeStorageSync(key) {
		return localStorage.removeItem(key)
	},
	//设置token
	setToken(token) {
		this.stores.authStore?.setToken(token);
	},
	// 获取 token
	getToken() {
		return this.stores.authStore?.token || this.getStorageSync('token');
	},
	sortObjectDeep(obj) {
		if (typeof obj !== 'object' || obj === null) return obj;
		const sortedKeys = Object.keys(obj).sort();
		const result = {};
		for (const key of sortedKeys) {
			if (typeof obj[key] === 'object' && !Array.isArray(obj[key]) && obj[key] !== null) {
				result[key] = sortObjectDeep(obj[key]);
			} else {
				result[key] = obj[key];
			}
		}
		return result;
	},
	// 深度克隆 - uview拿来的
	deepClone(obj) {
		// 对常见的“非”值，直接返回原来值
		if ([null, undefined, NaN, false].includes(obj)) return obj;
		if (typeof obj !== "object" && typeof obj !== 'function') {
			//原始类型直接返回
			return obj;
		}
		var o = Object.prototype.toString.call(obj) === '[object Array]' ? [] : {};
		for (let i in obj) {
			if (obj.hasOwnProperty(i)) {
				o[i] = typeof obj[i] === "object" ? used.deepClone(obj[i]) : obj[i];
			}
		}
		return o;
	},
	// 消息提示
	msg: function (msg) {
		showToast(msg);
	},
	// 请求中提示
	showLoading: (msg = "加载中...") => {
		showLoadingToast({
			message: msg,
			forbidClick: true,
			duration: 0
		});
	},
	// 关闭 请求中提示
	hideLoading: () => {
		closeToast();
	},
	// 跳转登录
	goLogin() {
		console.log('跳转登录');
	},
	// 返回上一页
	back() {
		router.back();
	},
	//前进或后退
	go(index) {
		router.go(index);
	},
	href: function (url, options = {}) {
		const router = this.router;
		if (!router) {
			console.error('Router is not injected into used object.');
			return;
		}
		let type = options.type || 1; //跳转方式
		let params = options.params || {}; //跳转携带参数params
		let query = options.query || {}; //跳转携带参数query
		let time = options.time || 0; //延迟跳转
		let msg = options.msg || ''; //提示文字
		let login = options.login || ''; //验证登录
		//验证登录
		let token = this.getToken();
		if (!token && login) {
			used.goLogin();
			return false;
		}
		//提示文字
		if (msg) {
			used.msg(msg);
		}
		//跳转
		switch (type) {
			case 1:
				//普通跳转
				setTimeout(() => {
					router.push({
						path: url,
						params: params,
						query: query
					})
				}, time)
				break;
			case 2:
				//关闭当前页面跳转
				setTimeout(() => {
					router.replace({
						path: url,
						params: params,
						query: query
					})
				}, time)
				break;
			case 3:
				// 关闭所有页面并跳转（替换当前历史记录）
				router.replace({
					path: url,
					params: params,
					query: query
				});
				// 清除浏览器历史栈（可选）：强制回到新页面，无法后退
				window.history.pushState(null, '', window.location.pathname);
				break;
		}
		//跳转

	},
	// px转换rem单位 
	// 用于style样式中 插件无法自动转换
	// 需要动态计算的尺寸 vue绑定样式时使用
	pxToRem(px) {
		const baseSize = 75;
		return px / baseSize + 'rem';
	}
}
export default used